import UIKit
import SnapKit

@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {
    
    
    //   用户注册的一个协议
    optional func userWillRgister()
    
    optional func userWillLogin()
    
    
}

class VisitorLoginView: UIView {
    
    var VisitorViewDelegate : VisitorLoginViewDelegate?
    
    @objc private func loginBtnDidClik () {
        
        
        VisitorViewDelegate?.userWillLogin!()
        
        
      
        
        
    }
    
    private func registerBtnDidClick () {
        
        
        VisitorViewDelegate?.userWillRgister!()
        
        
    }
    
    
    func setupUIWitchInfo(imageName: String?, tipText: String){
        
        if let img = imageName {
            
            
            cireIcon.image = UIImage (named: img)
            
            largeHouse.hidden = true
            
            bringSubviewToFront(cireIcon)
            
        }else {
            
            
            
            startAnimation()
            
            
        }
        tipLabel.text = tipText
        
        
        
    }
    
    
    func startAnimation () {
        
        
        
        let base = CABasicAnimation(keyPath: "transform.rotation")
        
        
        base.repeatCount = MAXFLOAT
        
        base.toValue = 2 * M_PI
        
        
        base.duration = 10
        
        base.removedOnCompletion = false
        
        
        cireIcon.layer.addAnimation(base, forKey: nil)
        
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI () {
        
        
        addSubview(cireIcon)
        addSubview(backView)
        addSubview(largeHouse)
        
        addSubview(tipLabel)
        
        addSubview(loginBtn)
        addSubview(regisetBtn)
        //        addSubview(backView)
        //
        //       表示添加一条约束
        cireIcon.snp_makeConstraints { (make) -> Void in
            
            //        make.centerX.equalTo(self.snp_center)
            //     make.centerY.equalTo(self.snp_center)
            
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
            
            
        }
        
        
        
        largeHouse.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(cireIcon.snp_center)
            
            //       make.centerY.equalTo(self.snp_centerY)
            
        }
        
        tipLabel.snp_makeConstraints { (make) -> Void in
            
            
            make.centerX.equalTo(cireIcon)
            make.top.equalTo(cireIcon.snp_bottom).offset(15)
            
            make.width.equalTo(255)
            make.height.equalTo(40)
            
            
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(tipLabel.snp_left)
            
            make.top.equalTo(tipLabel.snp_bottom).offset(17)
            
            make.width.equalTo(100)
            
        }
        
        regisetBtn.snp_makeConstraints { (make) -> Void in
            
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            
        }
        
        backView.snp_makeConstraints { (make) -> Void in
            //            make.center.equalTo(cireIcon.snp_center)
            
            make.top.left.right.equalTo(self)
            
            make.bottom.equalTo(regisetBtn.snp_bottom)
            
            
        }
        
        
        //          self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        
        loginBtn.addTarget(self, action: "loginBtnDidClik", forControlEvents: .TouchUpInside)
    
    
    regisetBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchDragInside)
    
    }
    
    
    

    
    
    // 懒加载 加载子视图
    
    
    
    lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    lazy var cireIcon :UIImageView = UIImageView (image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    
    lazy var largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //
    //    lazy var tipLabel: UILabel = {
    //        let l = UILabel()
    //        l.font = UIFont.systemFontOfSize(14)
    //        l.text = "登录后,别人评论你的微薄别人评论你的微薄别人评论你的微"
    //        //设置行数
    //        l.numberOfLines = 0
    //        l.textColor = UIColor.darkGrayColor()
    //        l.textAlignment = .Center
    //        //设置大小
    //        l.sizeToFit()
    //        return l
    //    }()
    
    lazy var tipLabel: UILabel = {
        
        let l = UILabel()
        
        l.font = UIFont.systemFontOfSize(14)
        
        l.numberOfLines = 0
        
        l.text = "登陆后,别人评论你的微薄,别人评论你的微薄评论的玩儿"
        
        l.textColor = UIColor.darkGrayColor()
        
        l.textAlignment = .Center
        
        l.sizeToFit()
        
        
        return l
    }()
    
    
    
    
    
    lazy var loginBtn: UIButton = {
        
        //     登陆按钮
        
        let btn = UIButton()
        
        btn.setTitle("登陆", forState: .Normal)
        
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        //        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
        
        
        return btn
    }()
    
    
    //  注册按钮
    
    lazy var regisetBtn: UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("注册", forState: .Normal)
        
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        let image = UIImage(named: "common_button_white_disable")!
        let w = Int(image.size.width * 0.5)
        let h = Int(image.size.height * 0.5)
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
        
        
        
        
        return btn
        
    }()
    
    
    
    
}
